package com.vanzo.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import com.vanzo.Tpplication;
import com.vanzo.encryption.R;

/**
 * Created by tisen on 2018/1/20.
 */

public abstract class BaseActivity2 extends AppCompatActivity implements View.OnClickListener{

    private Bundle savedInstanceState;
    private boolean isGoBack;
    protected Tpplication tpplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        tpplication = (Tpplication) getApplication();
        setContentView(getLayoutId());
	}
	
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        getSupportActionBar().hide();
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);

        initView();
        initData();
        initListener();
        backBtn();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
//                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
//                            View.SYSTEM_UI_FLAG_FULLSCREEN |
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    private  void backBtn(){
        View view = findViewById(R.id.goback);
        if(view != null){
            view.setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.goback :
                if(isGoBack){
                    processClick(v);
                }else{
                    finish();
                }

                break;
            default:
                processClick(v);
        }
    }

    public void setGoBack(boolean isGoBack){
        this.isGoBack = isGoBack;
    }
    public Bundle getBundle(){
        return savedInstanceState;
    }

    /** ͨ��Class��ת���� **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /** ����Bundleͨ��Class��ת���� **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /** ͨ��Action��ת���� **/
    protected void startActivity(String action) {
        startActivity(action, null);
    }

    /** ����Bundleͨ��Action��ת���� **/
    protected void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    /**
     * ��䲼��
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * ��ʼ��View
     */
    protected abstract void initView();

    /**
     * ��ʼ������
     */
    protected abstract void initData();

    /**
     * view�����¼�
     */
    protected abstract void initListener();

    /**
     * view�ĵ���¼�
     */
    protected abstract void processClick(View v);



    /**
     *���ؼ��رյ�ǰ�Ľ���
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
